<?php
namespace app\controller;

use think\Request;
use think\facade\Db;
use app\model\Service;
use app\model\Bazaar;
use app\model\Zone;
use app\model\Page;

class ServicePlan extends Common
{
	/**
	 * 搜索落地页
	 *
	 * @return
	 */
	public function getDomainUrl(Request $request) {
		if($this->hasPermission($request->sessInfo, 'serviceplan:read')) {
			return ['code'=> 403, 'msg'=> '你没有权限执行此操作!'];;
		}

		$where = [];
		$sort = $request->param('sort', 'id', 'trim');
		$order = $request->param('order', 'desc', 'trim');

		if($sort == 'name') {
			$sort = 'url';
		}

		array_push( $where, ['companyId', '=', $request->sessInfo['companyId'] ] );

		$bazaar = $request->param('bazaar', '', 'trim');
		if( $bazaar ) {
			array_push( $where, ['bazaar', '=', $bazaar ] );
		}

		$name = $request->param('name', '', 'trim');
		if( $name ) {
			array_push( $where, [ 'name|url', 'like', "%{$name}%"]  );
		}

		$paginate = Db::name('cfg_service_plan')->field('id, name, url, wechat')->where($where)->order($sort, $order)->select()->toArray();
		//echo Db::getLastSql()); 
		foreach( $paginate as &$value ) {
			$value['count'] = Db::table('cfg_service')->where('id', 'in', $value['wechat'])->count();
			unset($value['wechat']);
		}

    $serviceList = Service::getList($request->sessInfo, false);
		foreach( $serviceList as &$value ) {
			$value['display'] = true;
		}

		return ['code'=> 0, 'msg'=> '', 'count'=> count($paginate), 'data'=> $paginate,
			'bazaar' => Bazaar::getList($request->sessInfo, false),
			'zone' => Zone::getList($request->sessInfo),
			'page' => Page::getList($request->sessInfo),
			'service' => $serviceList,
		];
	}

	/**
	 * 创建落地页
	 *
	 * @return
	 */
	public function create(Request $request) {
		if($this->hasPermission($request->sessInfo, 'serviceplan:save')) {
			return ['code'=> 403, 'msg'=> '你没有权限执行此操作!'];;
		}

    $wechatstr = '';
		$model = $request->param('model', '', 'trim');
		$pageId = $request->param('pageId', '', 'trim');
		$wechat = $request->param('wechat', '', 'trim');
		if(is_array($wechat)) {
			if( count($wechat) > 0) {
				$wechatstr = implode(',', $wechat);
			}
		}

		$wechatData = $request->param('wechatData', '', 'trim');
		if(is_array($wechatData)) {
			if( count($wechatData) > 0) {
				$wechatstr = implode(',', $wechatData);
			}
		}

		$zoneid = $request->param('zoneid', '', 'trim');
		if(is_array($zoneid)){
			$zoneid = serialize($zoneid);
		}
		$platform = $request->param('platform', '', 'trim');
		if(is_array($platform)){
			$platform = serialize($platform);
		}
		$bazaarId = $request->param('bazaarId', '', 'trim');
		if(is_array($bazaarId)){
			$bazaarId = serialize($bazaarId);
		}
		$wechatData = $request->param('wechatData', '', 'trim');
		if(is_array($wechatData)) {
			$wechatData = serialize($wechatData);
		}

		$siteQuery = Db::table('cfg_site')->field('id, bazaar, SiteName, DomainUrl')->where('companyId', $request->sessInfo['companyId'])->where('id', 'in', $pageId)->select()->toArray();
		if( is_array($siteQuery) ) {
			foreach( $siteQuery as $value ) {
				$planId = Db::table('cfg_service_plan')->where(['companyId' => $request->sessInfo['companyId'], 'url' => $value['DomainUrl']])->value('id');
				if(empty($planId)) {
					Db::name('cfg_service_plan')->insert([ 'companyId' => $request->sessInfo['companyId'], 'bazaar' => $value['bazaar'], 'name' => $value['SiteName'], 'url' => $value['DomainUrl'], 'wechat' => $wechatstr, 'datatime' => date('Y-m-d H:i:s')]);
				}
			}
		}

		return ['code'=>0, 'msg'=>'操作成功'];
	}

	/**
	 * 删除落地页
	 *
	 * @return
	 */
	public function deleteDomainUrl(Request $request) {
		if($this->hasPermission($request->sessInfo, 'serviceplan:delete')) {
			return ['code'=> 403, 'msg'=> '你没有权限执行此操作!'];;
		}
		$beginTime = microtime(true);
		$param = $request->param();
		if( !isset($param['id']) ) {
			return ['code'=> 403, 'msg'=> '操作失败!'];
		}
		$delArray = [];
		$idArray = explode(',', $param['id']);
		if(count($idArray) > 0) {
			foreach( $idArray as $value ) {
				array_push($delArray, Db::table('cfg_service_plan')->where('id', $value)->where('companyId', $request->sessInfo['companyId'])->value('name'));
				Db::table('cfg_service_plan')->where('companyId', $request->sessInfo['companyId'])->where('id', $value)->delete();
			}
			foreach( $delArray as $value ) {
				if( $value ) {
					$endTime = microtime(true);
					$this->cfg_write_oper($request, $request->sessInfo['token'], $request->sessInfo['companyId'], $request->sessInfo['nickname'],
					$request->sessInfo['username'], round($endTime - $beginTime, 3), '客服展示', '删除客服展示['.$value.']', 0, json_encode($param));
				}
			}
		}
		return ['code'=> 0, 'msg'=> '删除成功!'];
	}

	/**
	 * 微信列表
	 *
	 * @return
	 */
	public function getWechat(Request $request) {
		if($this->hasPermission($request->sessInfo, 'serviceplan:read')) {
			return ['code'=> 403, 'msg'=> '你没有权限执行此操作!'];;
		}

		$where = [];
		$limit = $request->param('limit/d', 20);
		$sort = $request->param('sort', 'id', 'trim');
		$order = $request->param('order', 'desc', 'trim');
		$serviceId = $request->param('serviceId', '', 'trim');

		array_push( $where, ['companyId', '=', $request->sessInfo['companyId'] ] );

		$name = $request->param('name', '', 'trim');
		if( $name ) {
			array_push( $where, ['name', 'like',  "%{$name}%" ] );
		}

		$wechat = $request->param('wechat', '', 'trim');
		if( $wechat ) {
			array_push( $where, ['wechat', 'like',  "%{$wechat}%" ] );
		}

		$nickname = $request->param('nickname', '', 'trim');
		if( $nickname ) {
			array_push( $where, ['nickname', 'like',  "%{$nickname}%" ] );
		}

		$wechatId = Db::table('cfg_service_plan')->where('companyId', $request->sessInfo['companyId'])->where('id', $serviceId)->value('wechat');
		if(empty($wechatId)){
			return ['code'=> 0, 'msg'=> '', 'count'=> 0, 'data'=> []];
		}

        array_push( $where, ['id', 'in',  $wechatId] );
        $authority = true;
		$authaccount = $this->getWechatrole($request);
		if($authaccount) {
			array_push( $where, ['id', 'in',  $authaccount ] );
			$authority = false;
		}
		$query = Db::name('cfg_service')->field('id, platform, bazaarId, zoneid, name, nickname, wechat, phone, status, qrcode, sex, number, lasttime, datetime')->where($where)->order($sort, $order)->paginate($limit);

		return ['code'=> 0, 'msg'=> '', 'count'=> $query->total(), 'data'=> $query->items()];
	}
	
	 
	protected function getWechatrole($request)	{
		$roleId = Db::table('cfg_user')->where('id', $request->sessInfo['id'])->value('role');
		if(empty($roleId)) {
			return false;
		}
		$query = Db::table('cfg_role')->where('companyId', $request->sessInfo['companyId'])->where('id', $roleId)->value('authwechat');
		if(empty($query)) {
			return false;
		}
		return $query;
	}
	/**
	 * 微信查询
	 *
	 * @return
	 */
	public function getWechatSelected(Request $request) {
		if($this->hasPermission($request->sessInfo, 'serviceplan:read')) {
			return ['code'=> 403, 'msg'=> '你没有权限执行此操作!'];;
		}
		$where = [];
		$serviceId = $request->param('serviceId', '', 'trim');
		if( $serviceId ) {
			array_push( $where, ['id', '=',  $serviceId ] );
		}
		array_push( $where, ['companyId', '=', $request->sessInfo['companyId'] ] );
		$wechat = [];
		$query = Db::name('cfg_service_plan')->field('id')->where($where)->value('wechat');
		if($query) {
			$wechat = explode(',', $query);
		}
		return ['code'=> 0, 'msg'=> '', 'data'=> $wechat ];
	}

	/**
	 * 微信状态
	 *
	 * @return
	 */
	public function setStatus(Request $request) {
		if($this->hasPermission($request->sessInfo, 'serviceplan:read')) {
			return ['code'=> 403, 'msg'=> '你没有权限执行此操作!'];;
		}
		$id = $request->param('id', '', 'trim');
		if($id) {
			$status = $request->param('status', '', 'trim');
			Db::name('cfg_service')->where('id', $id)->where('companyId', $request->sessInfo['companyId'])->update(['status' => $status]);
		}
		return ['code'=>0, 'msg'=>'操作成功'];
	}

	/**
	 * 删除微信
	 *
	 * @return
	 */
	public function deleteWechat(Request $request) {
		if($this->hasPermission($request->sessInfo, 'serviceplan:delete')) {
			return ['code'=> 403, 'msg'=> '你没有权限执行此操作!'];;
		}

		$id = $request->param('id', '', 'trim');
		if(empty($id)) {
			return ['code'=> 0, 'msg'=> '删除成功!'];
		}

 		$idArray = explode(',', $id);
		if( count($idArray) == 0 ) {
			return ['code'=> 0, 'msg'=> '删除成功!'];
		}

		$serviceId = $request->param('serviceId', '', 'trim');
		if(empty($serviceId)) {
			return ['code'=> 0, 'msg'=> '删除成功!'];
		}

		$wechatId = Db::table('cfg_service_plan')->where('companyId', $request->sessInfo['companyId'])->where('id', $serviceId)->value('wechat');
		if(empty($wechatId)) {
			return ['code'=> 0, 'msg'=> '删除成功!'];
		}

		$idWechatId = explode(',', $wechatId);
		if( count($idWechatId) == 0 ) {
			return ['code'=> 0, 'msg'=> '删除成功!'];
		}

		$wechat = [];
		foreach( $idWechatId as $value ) {
			if(!in_array($value, $idArray)) {
				array_push($wechat, $value);
			}
		}

		Db::name('cfg_service_plan')->where('id', $serviceId)->update(['wechat' => implode(',', $wechat)]);

		return ['code'=> 0, 'msg'=> '删除成功!', 'count' => count($wechat) ];
	}
	
	/**
	 * 编辑微信
	 *
	 * @return
	 */
	public function editWechat(Request $request) {
		if($this->hasPermission($request->sessInfo, 'serviceplan:edit')) {
			return ['code'=> 403, 'msg'=> '你没有权限执行此操作!'];;
		}
		$serviceId = $request->param('serviceId', '', 'trim');
		if(empty($serviceId)) {
			return ['code'=> 0, 'msg'=> '操作成功!', 'count' => 0];
		}
		$wechatId = $request->param('wechatData', '', 'trim');
		if(empty($wechatId)) {
			return ['code'=> 0, 'msg'=> '操作成功!', 'count' => 0];
		}
		if( count($wechatId) == 0 ) {
			return ['code'=> 0, 'msg'=> '操作成功!', 'count' => 0];
		}
		Db::name('cfg_service_plan')->where('companyId', $request->sessInfo['companyId'])->where('id', $serviceId)->update(['wechat' => implode(',', $wechatId)]);
		$count = 0;
		$wechatStr = Db::table('cfg_service_plan')->where('companyId', $request->sessInfo['companyId'])->where('id', $serviceId)->value('wechat');
		if($wechatStr) {
			$wechatArray = explode(',', $wechatStr);
			foreach( $wechatArray as $value ) {
				if($value) {
					$count++;
				}
			}
		}
		return ['code'=> 0, 'msg'=> '操作成功!', 'count' => $count ];
	}
	
}
